package com.wood.life.rabbitma;


import com.night.common.entity.life.Image;
import com.night.common.entity.life.Question;
import com.night.common.entity.life.QuestionVo;
import com.rabbitmq.client.Channel;
import com.wood.life.mapper.ImageMapper;
import com.wood.life.mapper.QuestionMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * author: dz
 * 2022-09-22-11:13
 */

@Component
@Transactional
public class QuestionConsumer {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ImageMapper imageMapper;
    //处理消息可以添加三个参数
    //String str  消息正文
    //Message message 消息对象
    //Channel channel 信道

    @RabbitListener(queues = "queuequestion")
    public void receiveDelayplugin(QuestionVo questionVo, Message message, Channel channel) throws IOException {
        System.out.println("question:" + questionVo);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        System.out.println("查询修改订单状态：" + new Date());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("--------");
        questionMapper.insert(questionVo.getQuestion());
        Question question = questionVo.getQuestion();

        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(date.format(new Date()));// new Date()为获取当前系统时间
        question.setQuestionTime(date.format(new Date()));

        question.setTop("y");
        question.setStatus("y");
        questionMapper.insert(question);
        System.out.println("添加问题");

        Image image = new Image();
        image.setImageUrl(questionVo.getImageUrl());
        image.setType("life");
        image.setTypeId(question.getId());
        imageMapper.insert(image);
        System.out.println("成功");

    }
}
